package com.pd.backend.service.impl;

import com.pd.backend.domain.PdCoreOrgTreeBuild;
import com.pd.backend.dtos.PdCoreOrgDto;
import com.pd.backend.dtos.ResponseResult;
import com.pd.backend.entity.PdCoreOrg;
import com.pd.backend.entity.PdCoreOrgTree;
import com.pd.backend.enums.AppHttpCodeEnum;
import com.pd.backend.mapper.PdCoreOrgMapper;
import com.pd.backend.service.PdCoreOrgService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 组织(部门)业务层实现类
 * @Auther : 菜鸡编程
 * @Date:2023/1/12
 * @Description:
 * @VERSON:1.8
 */
@Service
@Slf4j
@Transactional
public class PdCoreOrgServiceImpl implements PdCoreOrgService {

    @Autowired
    private PdCoreOrgMapper pdCoreOrgMapper;

    /**
     * 机构作业范围列表
     * @return
     */
    @Override
    public ResponseResult pdCoreOrgGetTreeList(String name) {
        //校验参数(无需校验:空则查询所有,有则按条件查询)
        //处理业务
        //获取组织(部门)集合
        List<PdCoreOrgTree> pdCoreOrgTreeList = pdCoreOrgMapper.selectAllpdCoreOrg(name);
        //判断获取的集合是否存在
        if (Objects.isNull(pdCoreOrgTreeList)) {
            //集合不存在,返回错误消息:数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //创建树结构
        PdCoreOrgTreeBuild pdCoreOrgTreeBuild = new PdCoreOrgTreeBuild(pdCoreOrgTreeList);

        pdCoreOrgTreeList = pdCoreOrgTreeBuild.buildTree();

        //返回数据
        return ResponseResult.okResult(pdCoreOrgTreeList);
    }

    /**
     * 新增组织
     * @param pdCoreOrgDto
     * @return
     */
    @Override
    public ResponseResult addPdCoreOrg(PdCoreOrgDto pdCoreOrgDto) {
        //校验参数
        if (Objects.isNull(pdCoreOrgDto)) {
            //参数为空,返回错误消息:无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断组织名称是否存在
        if (StringUtils.isBlank(pdCoreOrgDto.getName())) {
            //组织名称不存在,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        //判断组织组织类型是否存在
        if (Objects.isNull(pdCoreOrgDto.getOrgType())){
            //组织类型不存在,返回错误消息:不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NOT_EMPTY);
        }
        //调用接口新增数据
        boolean result = pdCoreOrgMapper.addPdCoreOrg(pdCoreOrgDto);

        if (!result) {
            //false,返回错误消息:新增失败
            return ResponseResult.errorResult(AppHttpCodeEnum.DATE_INSERT_ERROR);
        }
        //新增成功
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据组织id查询组织
     * @param id
     * @return
     */
    @Override
    public ResponseResult selectPdCoreOrgById(Long id) {
        //校验参数
        if (Objects.isNull(id)) {
            //id为空,返回错误消息:无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //调用接口查询组织
        PdCoreOrg pdCoreOrg =pdCoreOrgMapper.selectPdCoreOrgById(id);

        if (Objects.isNull(pdCoreOrg)) {
            //组织为空,返回错误消息:数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //返回数据
        return ResponseResult.okResult(pdCoreOrg);
    }


}
